﻿Imports MindFusion.Scheduling
Imports MindFusion.Scheduling.WinForms
Imports System.Drawing.Drawing2D

Public Class FrmMoto

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim szGroupe As String
    Private centered As StringFormat
    Private cleft As StringFormat
    Dim SelectedDate As Date

    Private Sub FrmMoto_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = LibelleMoto(gTypePlanning)
        ' définir l'orientation du texte
        centered = New StringFormat
        centered.Alignment = StringAlignment.Center
        centered.LineAlignment = StringAlignment.Center
        cleft = New StringFormat
        cleft.Alignment = StringAlignment.Near
        cleft.LineAlignment = StringAlignment.Center

        Dim resource As Resource = Nothing
        ' créer les groupes A, B, C, ...
        CAL_MOTOS.Resources.Clear()
        For i As Integer = 1 To 7
            resource = New Resource
            resource.Id = Chr(64 + i)
            resource.Name = "Groupe " & Chr(64 + i)
            If gTypePlanning = "A" Then
                Select Case i
                    Case 1
                        resource.Name &= vbCrLf & "8 h à 11 h"
                    Case 2
                        resource.Name &= vbCrLf & "12 h à 15 h"
                    Case 3
                        resource.Name &= vbCrLf & "15 h à 18 h"
                    Case 4
                        resource.Name &= vbCrLf & "18 h à 21 h"
                End Select
            End If

            CAL_MOTOS.Resources.Add(resource)
        Next
        resource = Nothing

        TE_DATE.Value = Now
        SelectedDate = Nothing
        LoadPlanning()

    End Sub

    Sub LoadPlanning()
        Dim nGroupe As Integer = 0
        Dim app As Appointment

        Me.Cursor = Cursors.WaitCursor
        ' effacer le calendrier
        CAL_MOTOS.BeginInit()
        CAL_MOTOS.Schedule.Clear()

        ' Lire les 21 jours du planning

        szSQL = "CALL spGet_PLA_MOTO_byDATES('" & gTypePlanning & "','" & Format(CAL_MOTOS.Date, "yyyy/MM/dd") & "','" & Format(CAL_MOTOS.EndDate, "yyyy/MM/dd") & "'," & gUserAcces & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        Do While Not rs.EOF
            app = New Appointment()
            app.StartTime = rs.Fields("date").Value
            app.EndTime = app.StartTime.AddDays(1)
            If gTypePlanning = "A" Then
                app.HeaderText = "P " & rs.Fields("dispo").Value & " / " & rs.Fields("total").Value & vbNewLine & _
                                 "R " & rs.Fields("route_dispo").Value & " / " & rs.Fields("route").Value & vbNewLine & nv(rs.Fields("libelle").Value)
            Else
                app.HeaderText = rs.Fields("dispo").Value & " / " & nv(rs.Fields("total").Value) & vbNewLine & nv(rs.Fields("libelle").Value)
            End If
            nGroupe = Asc(rs.Fields("groupe").Value) - 65
            app.Resources.Add(CAL_MOTOS.Resources(nGroupe))
            app.Id = rs.Fields("groupe").Value
            app.Tag = nv(rs.Fields("valide").Value)
            If app.Tag = "O" Then
                ' journée validée = vert pale
                app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightGreen)
            ElseIf PlanningHasPending(gTypePlanning, Format(rs.Fields("date").Value, "yyyy/MM/dd"), rs.Fields("groupe").Value) = True Then
                ' il y a des élèves provisoires dans ce planning
                app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightPink)
            ElseIf rs.Fields("total").Value Or rs.Fields("route").Value Then
                ' planning rempli = jaune pale
                app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightYellow)
            Else
                ' planning non rempli = couleur bleu ciel
                app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightBlue)
            End If
            app.AllowChangeEnd = False
            app.AllowChangeStart = False
            app.AllowMove = False
            app.AllDayEvent = True
            CAL_MOTOS.Schedule.Items.Add(app)
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        Me.Cursor = Cursors.Default

        CAL_MOTOS.EndInit()
        CAL_MOTOS.Selection.Reset()

    End Sub

    Private Sub cal_MOTOS_DateClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ResourceDateEventArgs) Handles CAL_MOTOS.DateClick

        ' double-click ? oui, insérer nouveau groupe
        If e.Clicks = 2 And e.Button = System.Windows.Forms.MouseButtons.Left Then
            If IsUserAuthorised(9) = False Then
                Exit Sub
            End If
            szSQL = "CALL spINS_PLA_MOTO('" & gTypePlanning & "','" & Format(e.Date, "yyyy/MM/dd") & "','" & e.Resource.Id & "'," & gUserAgence & ",'" & gUserCode & "')"
            If ExecQuery(ctDSN_AEM, szSQL) Then
                ' réaffiche le calendrier
                LoadPlanning()
            End If
        End If

    End Sub

    Private Sub cal_MOTOS_Draw(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.DrawEventArgs) Handles CAL_MOTOS.Draw

        Dim bounds As Rectangle = e.Bounds
        Dim g As MindFusion.Drawing.IGraphics = e.Graphics
        Dim Jour As Date = e.Date

        Select Case e.Element
            Case CustomDrawElements.ResourceViewRowHeader
                ' encadrer les nom des ressources
                Dim r As Resource = e.Resource

                g.FillRectangle(Brushes.White, e.Bounds)
                g.DrawString(r.Name, e.Style.HeaderFont, Brushes.Navy, _
                             New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), centered)
                g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right - 1, e.Bounds.Bottom - 1)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

            Case CustomDrawElements.ResourceViewTimelineCell

                '           Dim b As Rectangle = e.Bounds
                Dim text As String = e.Text
                Dim isWeek As Boolean = False

                ' texte de l'entête = Semaine nn
                If text = "Week" Then
                    isWeek = True
                    Dim week As Integer = DatePart(DateInterval.WeekOfYear, e.Date, , FirstWeekOfYear.Jan1)
                    text = String.Format("semaine {0}", week)
                End If
                ' colorier l'entête
                Dim bbrush As Brush = Brushes.White
                If e.Date = DateTime.Now.Date Then
                    bbrush = Brushes.Orange
                ElseIf e.Date.DayOfWeek = CAL_MOTOS.FirstWeekendDay Then
                    bbrush = Brushes.LightGray
                ElseIf IsHoliday(e.Date) Then
                    bbrush = Brushes.LightSalmon
                End If
                g.FillRectangle(bbrush, bounds)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

                Dim textBrush As Brush = New SolidBrush(e.Style.HeaderTextColor)
                Dim textDest As Rectangle = bounds
                Dim state As GraphicsState = Nothing

                ' Rotation du texte du sous-titre
                If Not isWeek Then
                    textDest = New Rectangle(bounds.X, bounds.Y, bounds.Height, bounds.Width)
                    state = e.Graphics.Save()

                    Dim off As Single = 0
                    If textDest.Width > textDest.Height Then
                        off = CType(Math.Max(textDest.Width, textDest.Height) / 2, Single)
                    Else
                        off = CType(Math.Min(textDest.Width, textDest.Height) / 2, Single)
                    End If
                    g.TranslateTransform(textDest.Left + off, textDest.Top + off)
                    g.RotateTransform(270)
                    g.TranslateTransform(-textDest.Left - off, -textDest.Top - off)
                End If

                Dim f As StringFormat
                If isWeek Then f = cleft Else f = centered

                g.DrawString(text, e.Style.HeaderFont, textBrush, _
                New RectangleF(textDest.X, textDest.Y, textDest.Width, textDest.Height), f)

                If Not isWeek Then
                    g.Restore(state)
                End If

                textBrush.Dispose()

            Case CustomDrawElements.ResourceViewCell
                ' colorier la colonne
                Dim bcolor As Brush = Brushes.White
                If Jour = DateTime.Now.Date Then
                    bcolor = Brushes.Orange
                ElseIf Jour.DayOfWeek = CAL_MOTOS.FirstWeekendDay Then
                    bcolor = Brushes.LightGray
                ElseIf IsHoliday(Jour) Then
                    bcolor = Brushes.LightSalmon
                End If
                g.FillRectangle(bcolor, bounds)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

        End Select

    End Sub

    Private Sub cal_MOTOS_ItemClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemMouseEventArgs) Handles CAL_MOTOS.ItemClick

        If System.Windows.Forms.MouseButtons.Left Then
            ' Get the clicked event
            Dim app As Appointment = e.Item 'as Appointment

            ' mémoriser la date du RDV sélectionner
            SelectedDate = app.StartTime

            ' double-click = Modifier
            If e.Clicks = 2 Then
                ' afficher les détails de la ligne NoLigne
                FrmMotoMAJ.pDate = app.StartTime
                gGroupe = app.Id
                FrmMotoMAJ.ShowDialog()
                ' réaffiche le planning
                LoadPlanning()
            End If
        End If

    End Sub

    Private Sub BO_PREC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_PREC.Click
        ' reculer le calendrier de 3 semaines
        CAL_MOTOS.EndDate = CAL_MOTOS.Date
        CAL_MOTOS.Date = CAL_MOTOS.Date.AddDays(-21)
        ' afficher la date courante
        TE_DATE.Value = CAL_MOTOS.Date
        ' afficher sur 5 semaines
        LoadPlanning()

    End Sub

    Private Sub BO_SUIVANT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_SUIVANT.Click

        ' avancer le calendrier de 5 semaines
        CAL_MOTOS.Date = CAL_MOTOS.EndDate
        CAL_MOTOS.EndDate = CAL_MOTOS.EndDate.AddDays(35)
        ' afficher la date courante
        TE_DATE.Value = CAL_MOTOS.Date
        ' afficher sur 5 semaines
        LoadPlanning()

    End Sub

    Private Sub CAL_MOTOS_ItemDeleting(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemConfirmEventArgs) Handles CAL_MOTOS.ItemDeleting
        ' interdire la suppression
        e.Confirm = False
    End Sub

    Private Sub TE_DATE_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TE_DATE.ValueChanged
        ' caler au lundi
        ' borner le calendrier à afficher
        CAL_MOTOS.Date = getLundiPrecedent(TE_DATE.Value)
        CAL_MOTOS.EndDate = CAL_MOTOS.Date.AddDays(21)
        LoadPlanning()
    End Sub

End Class